<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends IndexController {
    public function lists(){
		$type=I("request.type");
		if($type==1){
			$User=D("User");
			$data['name']=I("request.name");
			$data['truename']=I("request.truename");
			$data['password']=md5(I("request.password"));
			$data['image']=I("request.image");
			$data['register_time']=time();
			$data['telephone']=I("request.telephone");
			$data['money_available']=I("request.money_available");
			$data['sex']=I("request.sex");
			$data['birthday']=I("request.birthday");
			if($_FILES['image']!=null){
				$image=$_FILES['image'];
				header("content-type:text/html;charset=utf-8");
				$upload = new \Think\Upload(); // 实例化上传类
				$upload->maxSize = 1024 * 1024 * 10;// 设置附件上传大小 (-1) 是不限值大小
				$upload->exts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath = "./imageUserLog/";
				$upload->replace = true; //存在同名文件是否是覆盖
				// 是否使用子目录保存上传文件
				$upload->autoSub = true;
				$info = $upload->uploadOne($image);
				if (!$info) {
					$path= "";
				} else {
					$path= ltrim($info['savepath'], './') . $info['savename'] ;
					$data['image']=$path;
				}
			}

			$result=$User->add($data);
		}
		if($type==2){
			$this->actionUpdate();
			$this->redirect("User/lists");

		}

        $this->display();
    }

  public function add(){
	  $this->display();
  }



  public function get_user_list(){
  	  $User=D("User");
  	  $userInfo=$User->field("id,name,truename,sex,telephone,birthday,image,money_available,register_time")->select();
  		$result=array();

  	   foreach ($userInfo as $key => $value) {
  	   	$result[$key]=$value;
  	  	if($value['register_time']!=null){
  	  		$result[$key]['register_time']=date('Y-m-d-H:i:s',$value["register_time"]);
  	  }
  	  	}
 
  	  if(!$result){
  	  		echo json_encode(0);exit;
  	  }else{
  	  		echo json_encode($result);exit;

  	  }
  }

	//===============================用于显示编辑页面======================
	 public function update(){
		 $id=I('request.id');
		 setcookie("uid", $id, time()+3600);
		 $this->display();
	 }

	public function update_list(){
		$id= $_COOKIE['uid'];
		$User=D("User");
		$where="id=".$id;
		$result=$User->where($where)->field('id,name,truename,password,telephone')->find();
		if(!$result){
			echo json_encode(0);exit;
		}else{
			echo json_encode($result);exit;
		}

	}
	public function actionUpdate(){
		 $id=$_COOKIE['uid'];
		 $User=D("User");
		 $where="id=".$id;
		 $data['name']=I("request.name");
		 $data['truename']=I("request.truename");
		 $data['telephone']=I("request.telephone");
		 $User->where($where)->save($data);
	}

	///======================================删除商前台会员信息===============================================
	public function del(){
		$User=D("User");
		$id=I("request.id");
		$map['id']=$id;
		$result=$User->where($map)->delete();
		if($result){
			echo json_encode(1);exit;
		}else{
			echo json_encode(0);exit;
		}
	}


}